<?php
error_reporting(E_ALL);
include_once(__SITE_PATH."/Lib/simple_html_dom.php");
/**
 * 
 * Hack data from ANT.COM.KH
 * @coder Sophy sem
 *
 */
class AntDictionaryGrep{
	private	$array_Part_KH= array();
	private $array_Part_EN= array();
	private $array_Mean_KH= array();
	private $array_Mean_EN= array();
	private $array_Label = array();
	private $word;
	private $html;

	/*ALL "get" Function return ARRAY*/
	public function getMeanKH(){
		return $this->array_Mean_KH;
	}
	
	public function getMeanEN(){
		return $this->array_Mean_EN;
	}
	
	public function getWord(){
		return $this->word;	
	}

	public function getHTMLObject(){
		return $this->html;
	}
	
	/**
		return Array(), can be : idiom, phrasal verb, other meaning, related phrase .....
	***/
	public function getOthers(){
		return $this->array_Label;
	}	

	function __construct($word) {
		$this->word = $word;

		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, "http://dict.antkh.com/dictionaries/".$this->word.".aspx"); 
		//return the transfer as a string 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		// $output contains the output string 
		$output = curl_exec($ch); 
		$this->html = str_get_html($output); 
		curl_close($ch);

		$tmp_Label = array();
		$item = $this->html->find('div.r',0);
		$suffix = "_KH";
		$is_label = false;
		if(empty($item)) return;
		do{
	
			if($item->tag=='label'){
				$is_label = true;
			}

			if(!$is_label){
		
				if($item->class=='engtitle'){
					$suffix = "_EN";
				}
				$var_part = "array_Part".$suffix;
				$var_mean = "array_Mean".$suffix;

				if($item->tag=='div' && $item->class=='r'){
				  $this->{$var_part}[]=$item->plaintext; 
				}
				if($item->tag=='dl'){
					foreach ($item->find('dd') as $dd_mean){
						$partOfSpeech = end($this->$var_part);
						$this->{$var_mean}[$partOfSpeech][] = $dd_mean->plaintext;
					}
				}
			}else{
		
				if($item->tag=='label'){
					$tmp_Label[] = $item->plaintext;
				}
				if($item->tag=='ul'){
					foreach ($item->find('li') as $li){
						$this->array_Label[end($tmp_Label)][]=$li->plaintext;
					}	
				}
				if($item->tag=='dl'){
					foreach ($item->find('dd') as $dd){
						$this->array_Label[end($tmp_Label)][]=$dd->plaintext;
					}	
				}
			}
			$item = &$item->next_sibling();

		}while(!empty($item));
		
	}

	public function getData(){
		/*
		return 			'{ 	"word"	: '.var_export($this->word,1).',
							"khmer" : '.var_export($this->array_Mean_KH,1).',
							"english" : '.var_export($this->array_Mean_EN,1).',
							"other" : '.var_export($this->array_Label,1).'
						}';*/
		$array_return_KH = array();
		$array_return_EN = array();		

		foreach(array("KH","EN") as $ext){
			foreach($this->{'array_Mean_'.$ext} as $key=>$values){
				${'array_return_'.$ext}[] = array("type"=>$key,"meaning"=>$values);
			}
		}		
		
		return (array('word'=>$this->word,
								 'khmer'=>$array_return_KH,
								 'english'=>$array_return_EN,
								 'other'=>$this->array_Label)
							);
	}
	 
	function __toString(){

		return var_export	(	array 	( 	"WORD"	=> $this->word,
											"KH" 	=> $this->array_Mean_KH,
											"EN" 	=> $this->array_Mean_EN,
											"OTHER" => $this->array_Label
										),
								true
							);
	}
}

/*
header('Content-Type: text/html; charset=utf-8');

$data = new AntDictionaryGrep('love');

echo "<pre>".$data->getData()."</pre>";
*/
?>

